package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.product.converter.dto.TrademarkConverter;
import com.cskaoyan.mall.product.converter.dto.TrademarkPageConverter;
import com.cskaoyan.mall.product.dto.TrademarkDTO;
import com.cskaoyan.mall.product.dto.TrademarkPageDTO;
import com.cskaoyan.mall.product.mapper.TrademarkMapper;
import com.cskaoyan.mall.product.model.Trademark;
import com.cskaoyan.mall.product.query.TrademarkParam;
import com.cskaoyan.mall.product.service.TrademarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/6/30 12:08
 */
@Service
public class TrademarkServiceImpl implements TrademarkService {

    @Autowired
    TrademarkMapper trademarkMapper;
    @Autowired
    TrademarkConverter trademarkConverter;
    @Autowired
    TrademarkPageConverter trademarkPageConverter;

    //1. 根据品牌id，查询品牌
    @Override
    public TrademarkDTO getTrademarkByTmId(Long tmId) {

        //1.1 进行查询
        Trademark trademark = trademarkMapper.selectById(tmId);
        //1.2 将PO对象转换为DTO对象
        TrademarkDTO trademarkDTO = trademarkConverter.trademarkPO2DTO(trademark);
        return trademarkDTO;
    }

    //2. 根据分页参数，分页查询品牌列表
    @Override
    public TrademarkPageDTO getPage(Page<Trademark> pageParam) {

        //2.1 分页查询，获取结果
        Page<Trademark> trademarkPage = trademarkMapper.selectPage(pageParam, null);
        //2.2 将PO对象转换为DTO对象
        TrademarkPageDTO trademarkPageDTO = trademarkPageConverter.tradeMarkPagePO2PageDTO(trademarkPage);

        return trademarkPageDTO;
    }

    //3. 保存品牌
    @Override
    public void save(TrademarkParam trademarkParam) {

        //3.1 将请求参数转换为PO对象
        Trademark trademark = trademarkConverter.trademarkParam2Trademark(trademarkParam);
        //3.2 进行数据的插入
        trademarkMapper.insert(trademark);
    }

    //4. 更新品牌
    @Override
    public void updateById(TrademarkParam trademarkParam) {
        //4.1 将请求参数转换为PO对象
        Trademark trademark = trademarkConverter.trademarkParam2Trademark(trademarkParam);
        //4.2 根据id进行更新记录
        trademarkMapper.updateById(trademark);
    }

    //5. 根据id删除品牌
    @Override
    public void removeById(Long id) {
        trademarkMapper.deleteById(id);
    }
}
